package com.fone.player.play.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.fone.player.ApplicationManage;
import com.fone.player.R;
import com.fone.player.client.DramaRst;
import com.fone.player.entity.OfflineCache;
import com.fone.player.storage.StorageModule;
import com.fone.player.util.FoneUtil;

import de.greenrobot.event.EventBus;

/**
 * 不规则剧集缓存ListView item的adapter
 * 
 * @author kongxiaojun
 * @since 2014-5-28
 */
public class UnRuleListCacheItemAdapter extends BaseAdapter {

	private List<DramaRst.Cnt> cntList;
	private static final int UPDATE_VIEW = 1; 
	private EventBus bus;
	
	public UnRuleListCacheItemAdapter(List<DramaRst.Cnt> cntList) {
		super();
		this.cntList = cntList;
		bus = new EventBus();
		bus.register(this);
		updateCacheState();
	}

	@Override
	public int getCount() {
		if (cntList == null) {
			return 0;
		}
		return cntList.size();

	}

	@Override
	public Object getItem(int position) {
		return cntList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public void setData(List<DramaRst.Cnt> cntList2) {
		this.cntList = cntList2;
		new Thread(new Runnable() {
			@Override
			public void run() {
				updateCacheState();
				Message msg = new Message();
				msg.what = UPDATE_VIEW;
				bus.post(msg);
			}
		}).start();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		DramaRst.Cnt cnt = cntList.get(position);

		ViewHolder holder;
		if (convertView == null) {
			convertView = LayoutInflater.from(ApplicationManage.getAplicationManageInstance()).inflate(R.layout.video_series_list_item, null);
			holder = new ViewHolder((TextView) convertView.findViewById(R.id.video_series_item_name), (ImageView) convertView.findViewById(R.id.cache_grid_status_iv));
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		// 已经选择，已经缓存，正在缓存
		if (cnt.cacheState != -1 && cnt.cacheState != OfflineCache.CACHE_STATE_FINISH) {// 正在缓存
			holder.seriesName.setTextColor(ApplicationManage.getAplicationManageInstance().getResources().getColor(R.color.video_title_bar_title_textcolor));
			holder.status.setImageResource(R.drawable.detail_download_finished_un);
		} else if (cnt.cacheState != -1 && cnt.cacheState == OfflineCache.CACHE_STATE_FINISH) {// 已经缓存
			holder.seriesName.setTextColor(ApplicationManage.getAplicationManageInstance().getResources().getColor(R.color.video_title_bar_title_textcolor));
			holder.status.setImageResource(R.drawable.detail_download_finished);
		} else if (cnt.isChecked == true) {// 已经选择
			holder.seriesName.setTextColor(ApplicationManage.getAplicationManageInstance().getResources().getColor(R.color.video_info_tab_line));
			holder.status.setImageResource(R.drawable.detail_download_choose);
		} else {// 其他
			holder.seriesName.setTextColor(ApplicationManage.getAplicationManageInstance().getResources().getColor(R.color.video_title_bar_title_textcolor));
			holder.status.setImageDrawable(new BitmapDrawable());
		}

		holder.seriesName.setText(cnt.name);
		convertView.setTag(holder);
		return convertView;
	}

	class ViewHolder {
		public TextView seriesName;
		public ImageView status;

		public ViewHolder(TextView seriesName, ImageView status) {
			super();
			this.seriesName = seriesName;
			this.status = status;
		}
	}

	/**
	 * 更新缓存状态
	 * 
	 * @return void
	 * @throws
	 */
	private void updateCacheState() {
		if (cntList != null && cntList.size() > 0) {
			for (DramaRst.Cnt cnt : cntList) {
				long cid = 0;
				OfflineCache cache = null;
				try {
					cid = Long.valueOf(FoneUtil.getCIdByUrl(cnt.url));
					cache = StorageModule.getInstance().getOfflineCacheFileByCID(cid);
				} catch (Exception e) {
					e.printStackTrace();
					cache = null;
				}
				if (cache == null) {
					cnt.cacheState = -1;
				} else {
					cnt.cacheState = cache.getCacheDownloadState();
				}
			}
		}
	}
	
	public void onEventMainThread(Message msg){
		switch (msg.what) {
		case UPDATE_VIEW:
			notifyDataSetChanged();
			break;
		default:
			break;
		}
	}
}
